package com.lmf.controller;

import com.lmf.vo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *  @Controller:创建处理器对象，对象放在springmvc容器中
 *  使用位置：在类的上面
 *  与spring框架中的用法一样
 */
@Controller
public class Mycontroller {
    /**
     * 逐个接收请求参数：
     *      要求：处理器方法的形参名和请求中参数名必须一致，同名的请求参数赋值给同名的形参
     * @param name
     * @param age
     * @return
     */
    @RequestMapping(value = "/first.do")
    public ModelAndView doSome(String name,Integer age){
        ModelAndView mv=new ModelAndView();
        mv.addObject("name", name);
        mv.addObject("age",age);
//        mv.setViewName("/WEB-INF/view/show.jsp");
        mv.setViewName("show");
        return mv;
    }

    /**
     * 请求中的参数名与处理器中的形参名不一样
     */
    @RequestMapping(value = "/receiveParam.do")
    public ModelAndView receiveParam(@RequestParam(value = "rname",required = false) String name,
                                      @RequestParam(value = "rage",required = false) Integer age){
        ModelAndView mv=new ModelAndView();
        mv.addObject("name", name);
        mv.addObject("age",age);
        mv.setViewName("show");
        return mv;
    }


    /**
     * 处理器方法形参是java对象，这个对象的属性值与请求中的参数名称一样；
     * 框架会创建形参java对象，给属性赋值，请求中的参数是name，框架会调用setName()
     * @return
     */
    @RequestMapping(value = "/receiveObject.do")
    public ModelAndView receiveObject(Student student){
        System.out.println("学生name："+student.getName()+"学生age："+student.getAge());
        ModelAndView mv=new ModelAndView();
        mv.addObject("name", student.getName());
        mv.addObject("age",student.getAge());
        mv.addObject("student",student);
//        mv.setViewName("/WEB-INF/view/show.jsp");
        mv.setViewName("show");
        return mv;
    }
}
